package com.lenovo.lcdm.dcm.service.impl;

import com.alibaba.excel.write.handler.SheetWriteHandler;
import com.alibaba.excel.write.handler.context.SheetWriteHandlerContext;
import com.lenovo.lcdm.dcm.constant.DcmConstant;
import lombok.extern.slf4j.Slf4j;
import org.apache.poi.ss.usermodel.DataValidation;
import org.apache.poi.ss.usermodel.DataValidationConstraint;
import org.apache.poi.ss.usermodel.DataValidationHelper;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.util.CellRangeAddressList;

import java.util.Set;

/**
 * @Auther xujing30
 * @Date 2024/01/31
 * @Description
 */
@Slf4j
public class FeaturesSheetWriteHandler implements SheetWriteHandler {

    private Set<String> categorySet;

    public FeaturesSheetWriteHandler(Set<String> categorySet){
        this.categorySet = categorySet;
    }

    @Override
    public void afterSheetCreate(SheetWriteHandlerContext context) {
        if(context.getWriteSheetHolder().getSheetName().equals("Features")) {
            log.info("第{}个Sheet写入Category下拉选项成功。", context.getWriteSheetHolder().getSheetNo());

            //设值category下拉选项设值范围
            Sheet sheet = context.getWriteSheetHolder().getSheet();
            CellRangeAddressList categoryCellRangeAddressList = new CellRangeAddressList(6, 100, 1, 1);
            DataValidationHelper helper = sheet.getDataValidationHelper();

            DataValidationConstraint categoryConstraint = helper.createExplicitListConstraint(categorySet.toArray(new String[categorySet.size()]));
            DataValidation categoryDataValidation = helper.createValidation(categoryConstraint, categoryCellRangeAddressList);
            sheet.addValidationData(categoryDataValidation);

            //设值geo下拉选项设值范围
            CellRangeAddressList geoCellRangeAddressList = new CellRangeAddressList(1, 1, 1, 1);
            DataValidationConstraint geoConstraint = helper.createExplicitListConstraint(DcmConstant.GEO);
            DataValidation geoDataValidation = helper.createValidation(geoConstraint, geoCellRangeAddressList);
            sheet.addValidationData(geoDataValidation);
        }
    }
}